var config = $.extend(true, {}, listApp, {
    data: {
        service: protocolcService,
        protocolId: '',
        stype: '',
        editIndex: -1,
        selfMidIndex: 0,
        addAble: true,
        isEdit: false,
        mids: [],
        originMids: [],
        contentHeight: 0,
        selfMid: '',
        selfChannelCode: '',
        selfChannelCodeIndex: 0,
        originChannelCodes: [],
        selectedRows: [],
        selectedIds: [],
        repeated: false,
        currentChannelId: '',
        tableVm: {
            pageSize: 1000
        },
        searchVm: {
            mid: '',
            name: '',
            protocolId: '',
            trap: 0,
            pageInfo: {
                orderField: 'channel_code',
                order: 'asc'
            }
        },
        options: [{
            label: '捷宸',
            value: '捷宸'
        }, {
            label: 'SNMP',
            value: 'snmp'
        }],
        smartImportReq: {
            deviceCode: '',
            protocolId: '',
            protocolName: '',
            protocolCIds: []
        },
        dataTypes: [
            {
                id: 0,
                name: '模拟量'
            },
            {
                id: 1,
                name: '开关量'
            }
        ],
        selectedIds: [],
        myHeaders: {},
        editVm: {
            bvisible: false,
            avisible: false,
            editFormRules: {
                formRules: {
                    mid: [{required: true, message: '请输入id', trigger: 'change'}],
                    name: [{required: true, message: '请输入名称', trigger: 'change'}],
                    unit: [{required: false}]
                }
            }
        },
        requestPagePath : 'requestPageExt'
    },
    methods: {
        init: function () {
            let fullUrl = window.location.href;
            this.id = this.getUrlKey("id", fullUrl);
            this.stype = this.getUrlKey("stype", fullUrl);
            this.protocolName = this.getUrlKey("protocolName", fullUrl);
            this.smartImportReq.protocolId = this.getUrlKey("id", fullUrl);
            this.smartImportReq.protocolName = this.getUrlKey("protocolName", fullUrl);
            this.initHeight();
            // console.log("protocolName",this.protocolName)
            this.searchVm.protocolId = this.id;
        },
        initHeight: function () {
            let that = this;
            setTimeout(() => {
                let height = document.documentElement.clientHeight;
                that.contentHeight = height - 190
                // console.log("height", that.contentHeight)
            }, 200)
        },
        dialogClose() {
            // let dialog = parent.window.document.getElementById("input_dialog")
            // dialog.click()
            this.editVm.bvisible = false
            this.doOneClick(this.editVm)
        },
        /**
         * 点击智能导入按钮
         */
        oneClick: function () {
            this.editVm.oprate = '提示'
            this.editVm.avisible = true
        },
        confirmOneClick: function () {
            this.editVm.oprate = '导入'
            this.editVm.avisible = false
            this.editVm.bvisible = true
            this.editVm.data.deviceCode = ''
        },
        doOneClick: function (e) {
            // console.log('e',e.data.deviceCode)
            const _this = this;
            this.editIndex = -1
            this.addAble = true
            this.smartImportReq.deviceCode = e.data.deviceCode
            this.editVm.bvisible = false
            this.tableVm.data.forEach((item) => {
                this.smartImportReq.protocolCIds.push(item.id)
            })
            _HTTP.post("/api/protocolc/smartImport", this.smartImportReq, {
                success(res) {
                    if (res.msg === 'success') {
                        _this.requsetPage();
                        _this.tableVm.data = _this.tableVm.data.sort(_this.sortDatas)
                    } else if (res.msg === 'errorDeviceCode') {
                        _this.showFailMsg("错误的通信识别码！")
                    }
                },
                fail(res) {
                    console.log(res)
                }
            })
        },
        sortDatas(a, b) {
            return a.dataType - b.dataType
        },
        maddRow: function () {
            this.getMaxChannelCode();
            this.isEdit = true;
            this.addAble = false;
            this.editIndex = 0;
            this.selectedIds = []
            this.selectedRows = []
            this.$refs.multipleTable.bodyWrapper.scrollTop = 0;
        },
        mdeleteRow: function (selectedRows) {
            var _this = this;
            var selectedIds = []
            selectedRows.forEach(row => {
                selectedIds.push(row.id)
            })
            this.$confirm('此操作将永久删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                _this.deleteLoading = true;
                let req = {
                    protocolId: _this.id,
                    protocolcIds: selectedIds
                }
                _HTTP.post("/api/protocolc/deleteRows", req, {
                    success(res) {
                        _this.deleteLoading = false;
                        _this.buildCurrentSearch();
                        _this.requsetPage();
                        _this.showSuccessMsg("操作成功！");
                    },
                    fail(res) {
                        _this.deleteLoading = false;
                        _this.showFailMsg(res.msg);
                    }
                })
            });
        },
        medit: function (index, row) {
            this.selfMid = row.mid;
            this.addAble = false
            this.selectedIds = []
            this.selectedRows = []
            this.selfChannelCode = row.channelCode;
            this.isEdit = true;
            this.editIndex = index;
            this.edit(index, row)
        },
        msave: function (index, row) {
            const _this = this;
            this.mids = this.originMids
            row.protocolId = this.id;
            row.protocolName = this.protocolName;
            row.trap = 0
            this.editVm.data = row;
            _HTTP.post("/api/protocolc/saveAProtocolc", row, {
                success(res) {
                    if(res.content === '保存成功！'){
                        _this.showSuccessMsg(res.content)
                        _this.requsetPage()
                        _this.isEdit = false;
                        _this.editIndex = -1;
                        _this.addAble = true;
                    }else {
                        _this.showFailMsg(res.content)
                    }
                },
                fail(res) {
                    console.log(res)
                }
            })
        },
        cancel: function (row) {
            this.isEdit = false;
            this.editIndex = -1;
            this.addAble = true;
            this.requsetPage();
        },
        handleSelectionChange: function (rows) {
            if (rows == null || rows.length <= 0) {
                this.selectedIds = []
                this.selectedRows = []
                return
            }
            if (this.isEdit) {
                this.selectedIds = []
                this.selectedRows = []
                return
            }
            let that = this;
            this.selectedRows = rows
        },
        /**
         * 返回上一页
         */
        goBack: function () {
            let url = top._CTX + '/web/protocol/main'
            let iframe = parent.window.document.getElementById("menuFrame")
            iframe.setAttribute("src", url)
        },
        getMaxChannelCode: function () {
            let that = this;
            // console.log(id)
            _HTTP.get("/api/protocolc/getMaxChannel?protocolId=" + this.id + "&trap=0", "", {
                success(res) {
                    console.log(res.content)
                    that.channelCode = res.content
                    if (that.channelCode != null && that.channelCode !== '') {
                        if (that.tableVm.data != null) {
                            let maxId = parseInt(that.channelCode)
                            for (let i = 1; i < 1000; i++) {
                                if (i > maxId) {
                                    if (i < 10) {
                                        that.currentChannelId = "00" + i
                                    } else if (i >= 10 && i < 100) {
                                        that.currentChannelId = "0" + i
                                    } else if (i >= 1000) {
                                        that.currentChannelId = '999'
                                    } else {
                                        that.currentChannelId = i
                                    }
                                    break;
                                }
                            }
                            // console.log("<<<<<<<<<<<<<<<<<<<<<")
                            // console.log(that.currentChannelId)
                            that.tableVm.data.splice(0, 0, {
                                channelCode: that.currentChannelId
                            });
                        } else {
                            // console.log(">>>>>>>>>>>>>>>>>>>>>>>>")
                            that.tableVm.data = {
                                channelCode: that.currentChannelId
                            }
                        }
                    } else {
                        // console.log("===========================================")
                        that.tableVm.data.splice(0, 0, {
                            channelCode: '001'
                        });
                    }
                },
                fail(res) {
                    console.log(res)
                }
            })
        },
        /**
         * 获取传参
         * @param name
         * @param url
         * @returns {string|null}
         */
        getUrlKey: function (name, url) {
            return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(url) || [, ""])[1].replace(/\+/g, '%20')) || null
        }
    }
});
var app = new Vue(config)